<?php
	/**
	* @File name:	upload.php
	* @Desription:	uplaod_interface
	* @Author:	 	jinxin<jx@178.com>   
	* @Date:	 	Wed Jul 20 17:26:07 CST 2011
	* @Copyright 	Copytright (c) 2008-2011 178.COM Co., Ltd.
	* @version	 	$Id $
	*/
	
	require_once('../bootstrap.php');
	$addr = $_SERVER['REMOTE_ADDR'];
	if (!in_array($addr,$__IPlist)){
		exit('no access!your ip is no rights to connect server');
	}
	$u = (int)$_GET['u'];										// 用户uid
	$d = (int)$_GET['d'];										// 日期
	$c = (int)$_GET['c'];										// 文件类型 0=>图片，1=>文件
	$ut = (int)$_GET['ut'];										// 用户类型 0=>普通用户，1=>后台编辑
	$uri = isset($_GET['uri']) ? (string)$_GET['uri'] : '';		// 图片路径
	$token = (string)$_GET['token'];							// 密钥
	$key = md5(md5($d.$u).$__key);
	if ($u < 1){
		exit('no access!your uid is wrong!');
	}
	if ($uri == ''){
		exit('no access!uri is no been null!');
	}
	if ($key != $token){
		exit('no access!your token is wrong!');
	}
	$path_root = $ut == 0 ? ($c == 0 ? PIC_PATH : FILE_PATH) : IMG_PATH;
	$attch_host = $ut == 0 ? ($c == 0 ? $__HOSTlist[2] : $__HOSTlist[1]) : $__HOSTlist[0];
	$pachInfo = parse_url($uri);
	$nameInfo = pathinfo($pachInfo['path']);
	switch ($c){
		case 0:
			if (!in_array($nameInfo['extension'],$__pic_c))
				exit('file\'s extension is not be allowed');
			break;
		default:
			if (!in_array($nameInfo['extension'],$__file_c))
				exit('file\'s extension is not be allowed');
			break;
	}
	$userPath = $ut == 0 ? $u.'/' : '';
	$fileName = $ut == 0 ? md5($nameInfo['filename'].uniqid()).'.'.$nameInfo['extension'] : $nameInfo['basename'];
	$filePath = $path_root.date('Ym').'/'.date('Ymd').'/'.$userPath;
	if (!is_dir($filePath)){
		mkdir($filePath,0775,true);
	}
	$fileUri = $attch_host.'/'.date('Ym').'/'.date('Ymd').'/'.$userPath.$fileName;
	$res = file_get_contents($uri);
	file_put_contents($filePath.$fileName,$res);
	exit($fileUri);
	